/*
 * Singame.java
 *
 * Created on 16 nov. 2010, 11:17:40
 */
package com.darrylsite.ihm;

import com.darrylsite.core.AppConfig;
import com.darrylsite.core.Modem;
import com.darrylsite.core.TimedMessageProcessor;
import com.darrylsite.entite.Message;
import com.darrylsite.ihm.tools.Utils;
import com.darrylsite.log.MyLogger;
import com.darrylsite.task.VisualModemInfoUpdater;
import java.awt.Component;
import java.io.IOException;
import java.util.logging.Level;
import javax.swing.ImageIcon;
import org.smslib.GatewayException;
import org.smslib.SMSLibException;

/**
 * @author nabster
 */
public class Singame extends javax.swing.JFrame implements ChangeModemListener
{
    
    public static Singame Application;
    private VisualModemInfoUpdater modemUpdater;

    /** Creates new form Singame */
    public Singame()
    {
        initComponents();
        setTitle();
        handleAction();
        centerPanel.add(new MessageDisplayer());
        launchThreads();
        Singame.Application = this;
        
    }
    
    private void addPanel(Component cmp)
    {
        this.centerPanel.removeAll();
        this.centerPanel.add(cmp);
        this.centerPanel.revalidate();
    }
    
    public VisualModemInfoUpdater getModemUpdater()
    {
        return modemUpdater;
    }
    
    public void setModemUpdater(VisualModemInfoUpdater modemUpdater)
    {
        this.modemUpdater = modemUpdater;
    }
    
    private void setTitle()
    {
        jTaskPaneGroup1.setTitle("Reglages");
        jTaskPaneGroup2.setTitle("Gestion des contacts");
        jTaskPaneGroup3.setTitle("Gestion des messages");
        jTaskPaneGroup4.setTitle("Etat du modem");
    }
    
    private void handleAction()
    {
        Utils util = new Utils();
        jLabel2.setIcon(new ImageIcon(util.loadImage("signal.png")));
        jLabel2.setText(" : ");
    }
    
    private void launchThreads()
    {
        Thread t = new Thread(new IHMUpdater());
        t.start();
    }
    
    private void doConnexion()
    {
        CreateConnexion p = new CreateConnexion();
        p.addChangeModemListener(this);
        addPanel(p);
    }
    
    private void addSenderIHM()
    {
        SenderIHM sender = IHMContext.getIhmSender();
        if (sender == null)
        {
            sender = new SenderIHM();
            IHMContext.setIhmSender(sender);
        }
        addPanel(new SenderIHM());
    }
    
    public void addSenderIHM(String number, Message msg)
    {
        SenderIHM sender;
        sender = new SenderIHM(number, msg);
        IHMContext.setIhmSender(sender);
        
        addPanel(sender);
    }
    
    public void showToSentIHM()
    {
        MessageToSentIHM ihm = IHMContext.getIhmToSent();
        if (ihm == null)
        {
            ihm = new MessageToSentIHM();
            IHMContext.setIhmToSent(ihm);
        }
        addPanel(ihm);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        topPanel = new javax.swing.JPanel();
        jButtonBar1 = new com.l2fprod.common.swing.JButtonBar();
        leftPanel = new javax.swing.JPanel();
        jTaskPane1 = new com.l2fprod.common.swing.JTaskPane();
        jTaskPaneGroup1 = new com.l2fprod.common.swing.JTaskPaneGroup();
        lblCreateConnex = new com.l2fprod.common.swing.JLinkButton();
        jTaskPaneGroup2 = new com.l2fprod.common.swing.JTaskPaneGroup();
        lblGrp = new com.l2fprod.common.swing.JLinkButton();
        lblContact = new com.l2fprod.common.swing.JLinkButton();
        jTaskPaneGroup3 = new com.l2fprod.common.swing.JTaskPaneGroup();
        lblNewMsg = new com.l2fprod.common.swing.JLinkButton();
        lblReceive = new com.l2fprod.common.swing.JLinkButton();
        lblToSend = new com.l2fprod.common.swing.JLinkButton();
        lblSent = new com.l2fprod.common.swing.JLinkButton();
        lblTrash = new com.l2fprod.common.swing.JLinkButton();
        jTaskPaneGroup4 = new com.l2fprod.common.swing.JTaskPaneGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btConnexion = new javax.swing.JToggleButton();
        centerPanel = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        topPanel.setBackground(new java.awt.Color(204, 204, 204));
        topPanel.setMinimumSize(new java.awt.Dimension(100, 60));
        topPanel.setPreferredSize(new java.awt.Dimension(781, 60));
        topPanel.setLayout(new java.awt.BorderLayout());
        topPanel.add(jButtonBar1, java.awt.BorderLayout.CENTER);

        getContentPane().add(topPanel, java.awt.BorderLayout.PAGE_START);

        leftPanel.setBackground(new java.awt.Color(102, 102, 102));
        leftPanel.setPreferredSize(new java.awt.Dimension(200, 373));
        leftPanel.setLayout(new java.awt.BorderLayout());

        lblCreateConnex.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/setting.png"))); // NOI18N
        lblCreateConnex.setText("Connexion");
        lblCreateConnex.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        lblCreateConnex.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        lblCreateConnex.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblCreateConnexMouseClicked(evt);
            }
        });
        jTaskPaneGroup1.getContentPane().add(lblCreateConnex);

        jTaskPane1.add(jTaskPaneGroup1);

        lblGrp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/groupedit.png"))); // NOI18N
        lblGrp.setText("Groupes");
        lblGrp.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        lblGrp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblGrpMouseClicked(evt);
            }
        });
        jTaskPaneGroup2.getContentPane().add(lblGrp);

        lblContact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/contact.png"))); // NOI18N
        lblContact.setText("Contacts");
        lblContact.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        lblContact.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblContactMouseClicked(evt);
            }
        });
        jTaskPaneGroup2.getContentPane().add(lblContact);

        jTaskPane1.add(jTaskPaneGroup2);

        lblNewMsg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/email_edit.png"))); // NOI18N
        lblNewMsg.setText("Nouveau sms");
        lblNewMsg.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        lblNewMsg.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblNewMsgMouseClicked(evt);
            }
        });
        jTaskPaneGroup3.getContentPane().add(lblNewMsg);

        lblReceive.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/email_open.png"))); // NOI18N
        lblReceive.setText("sms reçus");
        lblReceive.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        lblReceive.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblReceiveMouseClicked(evt);
            }
        });
        jTaskPaneGroup3.getContentPane().add(lblReceive);

        lblToSend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/email_send.png"))); // NOI18N
        lblToSend.setText("A envoyer");
        lblToSend.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        lblToSend.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        lblToSend.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblToSendMouseClicked(evt);
            }
        });
        jTaskPaneGroup3.getContentPane().add(lblToSend);

        lblSent.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/email.png"))); // NOI18N
        lblSent.setText("sms envoyés");
        lblSent.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        lblSent.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblSentMouseClicked(evt);
            }
        });
        jTaskPaneGroup3.getContentPane().add(lblSent);

        lblTrash.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/mail-trash.png"))); // NOI18N
        lblTrash.setText("Corbeille");
        lblTrash.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        lblTrash.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblTrashMouseClicked(evt);
            }
        });
        jTaskPaneGroup3.getContentPane().add(lblTrash);

        jTaskPane1.add(jTaskPaneGroup3);

        jLabel1.setText("Status :");
        jLabel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTaskPaneGroup4.getContentPane().add(jLabel1);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/signal.png"))); // NOI18N
        jLabel2.setText(" :");
        jLabel2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTaskPaneGroup4.getContentPane().add(jLabel2);

        btConnexion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/disconnect16.png"))); // NOI18N
        btConnexion.setText("Connexion");
        btConnexion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConnexionActionPerformed(evt);
            }
        });
        jTaskPaneGroup4.getContentPane().add(btConnexion);

        jTaskPane1.add(jTaskPaneGroup4);

        leftPanel.add(jTaskPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(leftPanel, java.awt.BorderLayout.LINE_START);

        centerPanel.setLayout(new java.awt.BorderLayout());
        getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lblNewMsgMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_lblNewMsgMouseClicked
    {//GEN-HEADEREND:event_lblNewMsgMouseClicked
        doNewMassageCliced();
    }//GEN-LAST:event_lblNewMsgMouseClicked
    
    private void lblReceiveMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_lblReceiveMouseClicked
    {//GEN-HEADEREND:event_lblReceiveMouseClicked
        doReceiveClick();
    }//GEN-LAST:event_lblReceiveMouseClicked
    
    private void lblToSendMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_lblToSendMouseClicked
    {//GEN-HEADEREND:event_lblToSendMouseClicked
        doToSendClick();
    }//GEN-LAST:event_lblToSendMouseClicked
    
    private void lblSentMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_lblSentMouseClicked
    {//GEN-HEADEREND:event_lblSentMouseClicked
        doSendClick();
    }//GEN-LAST:event_lblSentMouseClicked
    
    private void lblTrashMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_lblTrashMouseClicked
    {//GEN-HEADEREND:event_lblTrashMouseClicked
        doTrashClick();
    }//GEN-LAST:event_lblTrashMouseClicked
    
    private void lblCreateConnexMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_lblCreateConnexMouseClicked
    {//GEN-HEADEREND:event_lblCreateConnexMouseClicked
        doConnexion();
    }//GEN-LAST:event_lblCreateConnexMouseClicked
    
    private void lblGrpMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_lblGrpMouseClicked
    {//GEN-HEADEREND:event_lblGrpMouseClicked
        addPanel(new GroupeIHM());
    }//GEN-LAST:event_lblGrpMouseClicked
    
    private void lblContactMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_lblContactMouseClicked
    {//GEN-HEADEREND:event_lblContactMouseClicked
        addPanel(new ContactIHM());
    }//GEN-LAST:event_lblContactMouseClicked

    private void btConnexionActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btConnexionActionPerformed
    {//GEN-HEADEREND:event_btConnexionActionPerformed
       doStartModem();
    }//GEN-LAST:event_btConnexionActionPerformed
    
    private void doStartModem()
    {
        boolean b = false;
        try
        {
            AppConfig.modem.startModem();
            b = true;
        } catch (GatewayException ex)
        {
            MyLogger.getLogger().log(Level.WARNING, ex.getMessage(), ex);
        } catch (SMSLibException ex)
        {
            MyLogger.getLogger().log(Level.WARNING, ex.getMessage(), ex);
        } catch (IOException ex)
        {
            MyLogger.getLogger().log(Level.WARNING, ex.getMessage(), ex);
        } catch (InterruptedException ex)
        {
            MyLogger.getLogger().log(Level.WARNING, ex.getMessage(), ex);
        }
        
        btConnexion.setSelected(false);
    }
    
    private void doTrashClick()
    {
        MessageTashIHM rec = IHMContext.getIhmTrash();
        if (rec == null)
        {
            rec = new MessageTashIHM();
            IHMContext.setIhmTrash(rec);
        }
        
        addPanel(rec);
    }
    
    private void doSendClick()
    {
        MessageSentIHM rec = IHMContext.getIhmSent();
        if (rec == null)
        {
            rec = new MessageSentIHM();
            IHMContext.setIhmSent(rec);
        }
        
        addPanel(rec);
    }
    
    private void doToSendClick()
    {
        showToSentIHM();
    }
    
    private void doReceiveClick()
    {
        MessageReceiveIHM rec = IHMContext.getIhmReceiver();
        if (rec == null)
        {
            rec = new MessageReceiveIHM();
            IHMContext.setIhmReceiver(rec);
        }
        
        addPanel(rec);
    }
    
    private void doNewMassageCliced()
    {
        addSenderIHM();
    }

     @Override
    public void newConfig(String name, String port, int frequence)
    {
        if (AppConfig.modem != null)
        {
            AppConfig.modem.stopModem();
            if (AppConfig.timedProcessor != null)
            {
                AppConfig.timedProcessor.stop();
            }
        }  
        
        Modem modem = new Modem(port, frequence, "");
        AppConfig.modem = modem;
        if (AppConfig.timedProcessor == null)
        {
            AppConfig.timedProcessor = new TimedMessageProcessor(modem);
        }
        if (modemUpdater == null)
        {
            modemUpdater = new VisualModemInfoUpdater(modem);
            modemUpdater.setLblSignal(jLabel2);
            modemUpdater.setLblStatus(jLabel1);
        }
        modemUpdater.stop();
        modemUpdater.setModem(AppConfig.modem);
        modemUpdater.setModemName(name);
        modemUpdater.start();
        btConnexion.setSelected(false);
        btConnexion.doClick();
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            
            @Override
            public void run()
            {
                new Singame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btConnexion;
    private javax.swing.JPanel centerPanel;
    private com.l2fprod.common.swing.JButtonBar jButtonBar1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private com.l2fprod.common.swing.JTaskPane jTaskPane1;
    private com.l2fprod.common.swing.JTaskPaneGroup jTaskPaneGroup1;
    private com.l2fprod.common.swing.JTaskPaneGroup jTaskPaneGroup2;
    private com.l2fprod.common.swing.JTaskPaneGroup jTaskPaneGroup3;
    private com.l2fprod.common.swing.JTaskPaneGroup jTaskPaneGroup4;
    private com.l2fprod.common.swing.JLinkButton lblContact;
    private com.l2fprod.common.swing.JLinkButton lblCreateConnex;
    private com.l2fprod.common.swing.JLinkButton lblGrp;
    private com.l2fprod.common.swing.JLinkButton lblNewMsg;
    private com.l2fprod.common.swing.JLinkButton lblReceive;
    private com.l2fprod.common.swing.JLinkButton lblSent;
    private com.l2fprod.common.swing.JLinkButton lblToSend;
    private com.l2fprod.common.swing.JLinkButton lblTrash;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JPanel topPanel;
    // End of variables declaration//GEN-END:variables

   
}
